﻿Public Enum MemberType
    [Property]
    Field
End Enum
''' <summary>
''' Represents A Local from the Debugger
''' </summary>
''' <remarks></remarks>
''' <features></features>
''' <stepthrough></stepthrough>
<System.Diagnostics.DebuggerDisplay("LocalMember [Name: {Name}, TypeName: {TypeName}, Member: {Member}]")>
Public Class LocalMember
    Public Property Name As String
    Public Property Type As Type
    Public Property TypeName As String
    Public Property Member As MemberType
    Public Property HasGetAccessor As Boolean
    Public Property HasSetAccessor As Boolean

    Public ReadOnly Property ImageIndex() As Integer
        Get
            Return If(Member = MemberType.Field, 10, 6)
        End Get
    End Property

    Public ReadOnly Property CompilableTypeName() As String
        Get
            Return RepairTypeName(TypeName, Type)
        End Get
    End Property

    Public ReadOnly Property CompilableName() As String
        Get
            If Name = "this" Then
                Return "m___this"
            End If
            Return Name
        End Get
    End Property



    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="typeName"></param>
    ''' <param name="type"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Public Shared Function RepairTypeName(ByVal typeName As String, ByVal type As Type) As String
        If type IsNot Nothing AndAlso type.IsGenericType Then
            typeName = BuildGenericTypeName(type)
        End If

        Dim t As String = typeName.Replace("+", ".")
        If t.Contains("{") Then
            t = t.Substring(0, t.IndexOf("{") - 1)
        End If
        t = t.Replace("{", "").Replace("}", "").Trim()
        Return t
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="type"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <stepthrough></stepthrough>
    Private Shared Function BuildGenericTypeName(ByVal type As Type) As String
        If type Is Nothing OrElse type.FullName Is Nothing Then
            Return ""
        End If
        Dim name As String = type.FullName.Replace("+", ".")
        If type.IsGenericType Then
            If name.Contains("`") Then
                name = name.Substring(0, name.IndexOf("`"))
            End If
            Dim genArgString As String = ""
            Dim genArgs = type.GetGenericArguments()
            For i As Integer = 0 To genArgs.Length - 1
                genArgString += (If(i > 0, ", ", "")) & BuildGenericTypeName(genArgs(i))
            Next
            name += "<" & genArgString & ">"
        End If
        Return name
    End Function



    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Name">The Name of the Local [Example: Me, This]</param>
    ''' <param name="TypeName">The Name of the Local's Type</param>
    ''' <param name="Type">The Optional Type of the Local. Default = Nothing</param>
    ''' <remarks></remarks>
    ''' <stepthrough>Enabled</stepthrough>
    <DebuggerStepThrough()>
    Sub New(ByVal Name As String, ByVal TypeName As String, Optional ByVal Type As Type = Nothing)
        Me.Name = Name
        Me.TypeName = TypeName
        Me.Type = Type
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Name">The Name of the Local [Example: Me, This]</param>
    ''' <param name="TypeName">The Name of the Local's Type</param>
    ''' <param name="Member">The Type of Member it is</param>
    ''' <param name="Type">The Optional Type of the Local. Default = Nothing</param>
    ''' <remarks></remarks>
    ''' <stepthrough>Enabled</stepthrough>
    <DebuggerStepThrough()>
    Sub New(ByVal Name As String, ByVal TypeName As String, ByVal Member As MemberType, Optional ByVal Type As Type = Nothing)
        Me.Name = Name
        Me.TypeName = TypeName
        Me.Member = Member
        Me.Type = Type
    End Sub




End Class


''' <summary>
''' Represents A Local from the Debugger; With Extra Properties
''' </summary>
''' <remarks></remarks>
''' <features></features>
''' <stepthrough>Enabled</stepthrough>
<DebuggerStepThrough()>
<DebuggerDisplay("LocalMember [Name: {Name}, TypeName: {TypeName}, Value: {Value}, Data: {Data}, IsIEnumerable: {IsIEnumerable}]")>
Public Class LocalMember2
    Inherits LocalMember
    Public Property IsIEnumerable As TriState = TriState.UseDefault
    Public Property Data As EnvDTE100.Expression2
    Public Property Value As Object

    ''' <summary>
    ''' Returns all Items(As <see cref="EnvDTE100.Expression2">Expression2</see> From <see cref="Data">Data.DataMembers</see>)
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public ReadOnly Property Items As List(Of EnvDTE100.Expression2)
        Get
            Dim List As New List(Of EnvDTE100.Expression2)
            For Each Item In Data.DataMembers
                List.Add(Item)
            Next
            Return List
        End Get
    End Property


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Local">The Expression(COMOBject as <see cref="EnvDTE100.Expression2">Expression2</see>) Representation of A Local</param>
    ''' <remarks></remarks>
    ''' <stepthrough>Enabled</stepthrough>
    <DebuggerStepThrough()>
    Sub New(ByVal Local As EnvDTE100.Expression2)
        MyBase.New(Local.Name, Local.Type)
        Me.Data = Local
        Me.Value = Local.Value
    End Sub


End Class